package com.pic.others.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pic.common.utils.R;
import com.pic.common.utils.UserContext;
import com.pic.others.config.minio.MinIOConfigProperties;
import com.pic.others.config.minio.MinIOUtil;
import com.pic.others.domain.dto.recyle.RecyleFormDTO;
import com.pic.others.domain.dto.recyle.RecyleManyDTO;
import com.pic.others.domain.po.Picture;
import com.pic.others.domain.po.Recyle;
import com.pic.others.mapper.PictureMapper;
import com.pic.others.mapper.RecyleMapper;
import com.pic.others.service.IRecyleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author koshi
 * @since 2023-11-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RecyleServiceImpl extends ServiceImpl<RecyleMapper, Recyle> implements IRecyleService {

    private final MinIOUtil minIOUtil;
    private final MinIOConfigProperties minIOConfigProperties;
//    private final RecyleMapper recyleMapper;
//    private final IUserService userService;
//    private final ICategoryService categoryService;
//    private final CategoryMapper categoryMapper;
//    private final RabbitMqHelper mqHelper;
//    private final SaveSearchRecord saveSearchRecord;
    private final PictureMapper pictureMapper;
    private final PictureServiceImpl pictureService;
//    private final StringRedisTemplate redisTemplate;
//    private final RecommendSimilarPictureServiceImpl recommendSimilarPictureService;
//    private final RecommendSimilarPictureMapper recommendSimilarPictureMapper;
    @Value("${minio.bucket}")
    private String bucketName;


    @Override
    public void deleteByTime(LocalDateTime now) {
    remove(new QueryWrapper<Recyle>().lambda().lt(Recyle::getRecyleTime,now));
    }

    public R<List<RecyleFormDTO>> getRecylePicture() {
        Long userId = UserContext.getUser();
        List<Recyle> recyles = lambdaQuery().eq(Recyle::getUserId, userId).list();
        List<RecyleFormDTO> dtos=new ArrayList<>();
        for(Recyle recyle:recyles){
            RecyleFormDTO dto=new RecyleFormDTO();
            dto.setLocate(recyle.getLocate());
            dto.setPictureId(recyle.getPicId());
            dto.setRecyleId(recyle.getRecyleId());
            dto.setDay(Duration.between(recyle.getDateTime(), recyle.getRecyleTime()).toDays());
            dtos.add(dto);
        }
        Collections.sort(dtos, new Comparator<RecyleFormDTO>() {
            @Override
            public int compare(RecyleFormDTO dto1, RecyleFormDTO dto2) {
                return Long.compare(dto2.getDay(), dto1.getDay());
            }
        });


        return R.success(dtos);


    }

    @Override
    public R<String> deleteByRecyleTime(Long recyleId) {
        Recyle recyle = getById(recyleId);
        boolean flag1 = minIOUtil.deleteFile(bucketName, minIOConfigProperties.getReadPath()+recyle.getLocate());
        recyle=null;
        boolean flag2 = removeById(recyleId);
        if(flag1&&flag2){
            return R.success("删除成功");
        }
        return R.error("出现问题请联系管理员");
    }

    @Override
    public R<String> updatePictureRecyleStatus(Long recyleId) {
        Recyle recyle = getById(recyleId);
        Picture picture=new Picture();
        picture.setLocate(recyle.getLocate());
        picture.setDateTime(recyle.getDateTime());
        picture.setUserId(recyle.getUserId());
        boolean save = pictureService.save(picture);
        if(save){
            return R.success("恢复成功");
        }else{
            return R.error("出现错误请联系管理员");
        }
    }

    @Override
    public R<String> deleteByRecyleIds(List<Long> recyleIds) {
        for(Long recyleId:recyleIds){
            Recyle recyle=getById(recyleId);
            boolean flag1 = minIOUtil.deleteFile(bucketName, minIOConfigProperties.getReadPath()+recyle.getLocate());
            recyle=null;
            removeById(recyleId);

        }
        return R.success("删除成功");
    }

    @Override
    public R<String> updatePictureRecyleStatusByIds(List<Long> recyleIds) {
        for(Long recyleId:recyleIds){
            log.info(recyleId.toString());
            Recyle recyle = getById(recyleId);
            Picture picture=new Picture();
            picture.setLocate(recyle.getLocate());
            picture.setDateTime(recyle.getDateTime());
            picture.setUserId(recyle.getUserId());
            boolean save = pictureService.save(picture);
            removeById(recyleId);

        }
        return R.success("恢复成功");
    }

    @Override
    public R<String> deleteAllPictures() {
        Long userId = UserContext.getUser();
        if(userId==null){
            return R.error("请先登录");
        }
        List<Recyle> recyles = lambdaQuery().eq(Recyle::getUserId, userId).list();
        for(Recyle recyle:recyles){
            boolean flag1 = minIOUtil.deleteFile(bucketName, minIOConfigProperties.getReadPath()+recyle.getLocate());
            removeById(recyle.getRecyleId());
            recyle=null;

        }
        return R.success("删除成功");
    }

    @Override
    public R<RecyleManyDTO> getRecylesByRecyleId(Long recyleId) {
        Long userId = UserContext.getUser();
        if(userId==null){
            return R.error("请先登录");
        }
        RecyleManyDTO manyDTO=new RecyleManyDTO();
        Recyle recyle=getById(recyleId);
        manyDTO.setRecyleId(recyleId);
        manyDTO.setLocate(recyle.getLocate());

        List<Recyle> recyles = lambdaQuery().eq(Recyle::getUserId, userId).list();
        List<RecyleFormDTO> dtos=new ArrayList<>();
        for(Recyle rc:recyles){
            RecyleFormDTO formDTO=new RecyleFormDTO();
            formDTO.setLocate(recyle.getLocate());
            formDTO.setPictureId(recyle.getPicId());
            formDTO.setRecyleId(recyle.getRecyleId());
            formDTO.setDay(Duration.between(recyle.getDateTime(), recyle.getRecyleTime()).toDays());
            dtos.add(formDTO);
        }
        Collections.sort(dtos, new Comparator<RecyleFormDTO>() {
            @Override
            public int compare(RecyleFormDTO dto1, RecyleFormDTO dto2) {
                return Long.compare(dto2.getDay(), dto1.getDay());
            }
        });
        manyDTO.setRecyles(dtos);
        return R.success(manyDTO);
    }
}
